﻿using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Business.Reports;
using NP.BPMReportPlatform.Entities.BPM_Engine;
using NP.BPMReportPlatform.Reports.CommonApprovalProcessReport.Dtos;
using NP.BPMReportPlatform.Utilities;
using NP.BPMReportPlatform.Utils;
using NPOI.SS.UserModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Reports.CommonApprovalProcessReport
{
    public class CBDZDZGLService : BPMReportPlatformAppService, ICBDZDZGLService
    {
        private readonly SqlSugarClient _db, _dbPlatform, _dbProcess, _dbEngine, _dbToDoCenter;
        private readonly IBPMUtilsAppService _bPMUtilsApp;
        public CBDZDZGLService(ISqlSugarAppService sqlSugarAppService, IBPMUtilsAppService bPMUtilsApp)
        {
            _db = sqlSugarAppService.GetBusinessInstance();
            _dbPlatform = sqlSugarAppService.GetPlatformInstance();
            _bPMUtilsApp = bPMUtilsApp;
            _dbProcess = sqlSugarAppService.GetProcessInstance();
            _dbEngine = sqlSugarAppService.GetEngineInstance();
            _dbToDoCenter = sqlSugarAppService.GetToDoCenterInstance();
        }

        /// <summary>
        /// 修复数据
        /// </summary>
        /// <returns></returns>
        public async Task FixData()
        {
            var bussinessDatas = _db.Queryable<CommonApprovalProcess>()
                .Where(x => x.BTID == "CBDZDZGL").ToList();
            foreach (var bussinessData in bussinessDatas)
            {
                var instance = _dbEngine.Queryable<Instances>().First(x => x.Number == bussinessData.InstanceNumber);
                if (instance != null)
                {
                    instance.Topic = $"船舶电子底账管理通用流程：{bussinessData.Text1}，{bussinessData.Text2}";
                    _dbEngine.Updateable(instance).WhereColumns(x => x.InstanceId).ExecuteCommand();
                }
            }
        }

        public async Task<PagedResultDto<CommonApprovalTestSubData>> GetTestSubDataList(GetTestSubDataListInput input)
        {
            var query = _db.Queryable<CommonApprovalTestSubData>()
                .WhereIF(!string.IsNullOrEmpty(input.InstanceNumber), x => x.InstanceNumber == input.InstanceNumber)
                .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name == input.Name);
            RefAsync<int> total = 0;
            var data = await query.ToPageListAsync(input.PageIndex, input.PageSize, total);
            return new PagedResultDto<CommonApprovalTestSubData>()
            {
                TotalCount = total,
                Items = data
            };
        }

        public async Task<List<CommonApprovalTestSubData>> ExportTestSubDataList(GetTestSubDataListInput input)
        {
            var query = _db.Queryable<CommonApprovalTestSubData>()
                .WhereIF(!string.IsNullOrEmpty(input.InstanceNumber), x => x.InstanceNumber == input.InstanceNumber)
                .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name == input.Name);
            var data = await query.ToListAsync();
            return data;
        }
    }
}
